<template>
  <div>
    <el-input v-model="searchKeyword" placeholder="搜索用户" style="width: 300px; margin-bottom: 20px;"></el-input>
    <el-button type="primary" @click="searchUsers">搜索</el-button>
    <el-button type="success" @click="openAddUserDialog">新增用户</el-button>

    <el-table :data="users" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="userId" label="用户ID" width="100"></el-table-column>
      <el-table-column prop="username" label="用户名" width="150"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
      <el-table-column prop="registrationDate" label="注册日期" width="180"></el-table-column>
      <el-table-column prop="lastLogin" label="最后登录" width="180"></el-table-column>
      <el-table-column prop="role" label="角色" width="100"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="editUser(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增用户对话框 -->
    <el-dialog title="新增用户" :visible.sync="addDialogVisible">
      <el-form :model="newUser">
        <el-form-item label="用户名">
          <el-input v-model="newUser.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="newUser.email"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="newUser.password"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="newUser.role" placeholder="请选择角色">
            <el-option label="管理员" value="2"></el-option>
            <el-option label="用户" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addNewUser">保存</el-button>
      </div>
    </el-dialog>

    <!-- 编辑用户对话框 -->
    <el-dialog title="编辑用户" :visible.sync="editDialogVisible">
      <el-form :model="currentUser">
        <el-form-item label="用户名">
          <el-input v-model="currentUser.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="currentUser.email"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="currentUser.password"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="currentUser.role" placeholder="请选择角色">
            <el-option label="管理员" value="2"></el-option>
            <el-option label="用户" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updateUser">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchAllUsers, searchUsers, updateUser, deleteUser, addUser } from '@/common/api';

export default {
  data() {
    return {
      users: [],
      searchKeyword: '',
      editDialogVisible: false,
      addDialogVisible: false,  // 新增用户对话框的显示状态
      newUser: {  // 新增用户的数据模型
        username: '',
        email: '',
        password: ''

      },
      currentUser: {
        userId: '',
        username: '',
        email: '',
        role: ''
      }
    };
  },

  methods: {
    fetchUsers() {
      fetchAllUsers().then(response => {
        this.users = response.data;
      });
    },
    searchUsers() {
      searchUsers(this.searchKeyword).then(response => {
        this.users = response.data;
      });
    },
    openAddUserDialog() {
      this.newUser = { username: '', email: '', password: '' };  // 重置新增用户表单
      this.addDialogVisible = true;  // 显示新增用户对话框
    },
    addNewUser() {
      addUser(this.newUser).then(() => {
        this.fetchUsers();  // 刷新用户列表
        this.addDialogVisible = false;  // 关闭对话框
      }).catch(error => {
        console.error('新增用户失败', error);
      });
    },
    editUser(user) {
      this.currentUser = { ...user };
      this.editDialogVisible = true;
    },
    updateUser() {
      updateUser(this.currentUser.userId, this.currentUser).then(() => {
        this.fetchUsers();
        this.editDialogVisible = false;
      });
    },
    deleteUser(userId) {
      deleteUser(userId).then(() => {
        this.fetchUsers();
      });
    }
  },

  mounted() {
    const token = localStorage.getItem('token');
    console.log('登录 token 信息:', token);
    this.fetchUsers();
  }
};
</script>


<style scoped>
.el-input {
  margin-right: 10px;
}
.dialog-footer {
  text-align: right;
}
</style>
